package com.zja.dao.Impl;

import com.zja.dao.BaseDAO;
import com.zja.dao.KnowledgeBaseDao;
import com.zja.model.KnowledgeBase;

import java.util.List;

/**
 * @author JA
 */
public class KnowledgeBaseDaoImpl extends BaseDAO implements KnowledgeBaseDao {
    @Override
    public List<KnowledgeBase> selectAll() {
        String sql = "SELECT * FROM knowledge_base";
        try {
            return executeQuery(KnowledgeBase.class, sql);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public KnowledgeBase selectById(Long id) {
        String sql = "SELECT * FROM knowledge_base WHERE id = ?";
        try {
            return executeQueryBean(KnowledgeBase.class, sql, id);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public boolean insert(KnowledgeBase knowledgeBase) {
        String sql = "INSERT INTO knowledge_base(name, description, creator_id,  access_level, created_at, updated_at) VALUES( ?, ?, ?, ?, ?, ?)";
        try {
            return executeUpdate(sql, knowledgeBase.getName(), knowledgeBase.getDescription(), knowledgeBase.getCreatorId(),
                    knowledgeBase.getAccessLevel(), knowledgeBase.getCreatedAt(), knowledgeBase.getUpdatedAt());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public boolean update(KnowledgeBase knowledgeBase) {
        String sql = "UPDATE knowledge_base SET name = ?, description = ?, creator_id = ?,  access_level = ?, updated_at = ? WHERE id = ?";
        try {
            return executeUpdate(sql, knowledgeBase.getName(), knowledgeBase.getDescription(), knowledgeBase.getCreatorId(),
                     knowledgeBase.getAccessLevel(), knowledgeBase.getUpdatedAt(), knowledgeBase.getId());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public boolean deleteById(Long id) {
        String sql = "DELETE FROM knowledge_base WHERE id = ?";
        try {
            return executeUpdate(sql, id);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<Long> selectAdminIdsByKbId(Long kbId) {
        String sql = "SELECT user_id FROM knowledgebase_admins WHERE knowledge_base_id = ?";
        try {
            return executeQueryColumn(sql, kbId);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<KnowledgeBase> selectByAdminUserId(Long userId) {
        String sql = "SELECT k.* FROM knowledge_base k " +
                "JOIN knowledgebase_admin ka ON k.id = ka.knowledge_base_id " +
                "WHERE ka.user_id = ?";
        try {
            return executeQuery(KnowledgeBase.class, sql, userId);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<KnowledgeBase> selectAllByAdminUserId(Long userId) {
        String sql = "SELECT DISTINCT k.* " +
                "FROM knowledge_base k " +
                "LEFT JOIN knowledgebase_admin a ON k.id = a.knowledge_base_id " +
                "WHERE k.creator_id = ? OR a.user_id = ?";
        try {
            return executeQuery(KnowledgeBase.class, sql, userId, userId);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
